package jwebtest.rpc;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.Test;

import com.jweb.util.FormatUtil;

import jwebtest.rpc.rpc.IRpcFramework;
import jwebtest.rpc.rpc.scoket.ScoketRPC;
import jwebtest.rpc.server.IHelloServer;
import jwebtest.rpc.server.HelloServerImpl;
import jwebtest.rpc.zk.ZkApi;

public class JunitRpc {
	public static void log(String msg, Object... params) {
		System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()) + "\t" + Thread.currentThread().getName() + "\t"
				+ FormatUtil.formatParams(msg, params));
	}

	public static AtomicInteger NO = new AtomicInteger(0);

	@Test
	public void testConsumer() throws IOException, InterruptedException {
		ZkApi zkapi = new ZkApi();
		zkapi.createConnection("192.168.1.23:2181", 2000);
		final IRpcFramework rpc = new ScoketRPC(2000);
		rpc.addRegister(zkapi);
		rpc.initDomainList();
		// final IRpcFramework rpc = new ScoketRPC("127.0.0.1", 1234, 2000);
		for (int i = 0; i < 1; i++) {
			try {
				IHelloServer service = rpc.reference(IHelloServer.class);
				log("{} \t{}", NO.addAndGet(1), service.Say("yuanqiyong"));
				// Thread.sleep(20000);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		Thread.sleep(30000);
	}

	@Test
	public void testProvider() throws IOException {
		ZkApi zkapi = new ZkApi();
		zkapi.createConnection("192.168.1.23:2181", 2000);
		IRpcFramework rpc = new ScoketRPC(FormatUtil.getRandomInt(50) + 2048, 2000);
		rpc.addRegister(zkapi);
		rpc.addExport(new HelloServerImpl(), 1, FormatUtil.getRandomInt(100));
		while (true)
			;
	}
}
